<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>DMDirc :: Admin</title>
  <link rel="stylesheet" type="text/css" href="/res/style.css">
  <style type="text/css">
   table { border-collapse: collapse; width: 100%; }
   th, td { border: 1px solid #ccc; padding: 4px; }
  </style>
 </head>
 <body>
  <div id="container">
   <img id="logo" src="/res/logo.png" alt="DMDirc logo">
   <h1>DMDirc</h1>
<?PHP require_once('admin-menu.php'); ?>
   <div id="content">
    <h2>Update hits</h2>
<?PHP

$fh = fopen('/etc/apache2/logs/subdomain.dmdirc.com', 'r');

$hits = array();

while (!feof($fh)) {
 $line = fgets($fh);

 if (preg_match('/^(.*?) \- \- \[(.*?)\] "POST \/ /i', $line, $m)) {
  $ip = $m[1];
  $time = strtotime($m[2]);

  if (!isset($hits[$ip])) {
   $hits[$ip] = array('first' => $time, 'last' => '', 'hits' => 0);
  }

  $hits[$ip]['hits']++;
  $hits[$ip]['last'] = $time;
 }
}

function ds($a, $b) {
 return strnatcmp($a['hits'], $b['hits']);
}

uasort($hits, 'ds');

echo '<table><tr><th>IP</th><th>Hits</th><th>First hit</th><th>Last hit</th></tr>';

foreach ($hits as $ip => $dat) {
 echo '<tr>';
 echo '<td>' . htmlentities($ip) . '</td>';
 echo '<td>' . number_format($dat['hits']) . '</td>';
 echo '<td>' . date('r', $dat['first']) . '</td>';
 echo '<td style="';
 if ($dat['last'] < strtotime('-1 week')) {
  echo "color: red";
 } else if ($dat['last'] > strtotime('-25 hours')) {
  echo "color: green";
 }
 echo '">' . date('r', $dat['last']) . '</td>';
 echo '</tr>';
}

echo '</table>';
?>
   </div>
   <div id="footer">
   </div>
  </div>
 </body>
</html>

